package com.pengtu.web.asset.car;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.Constants;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.QueryFilter.MatchType;
import com.pengtu.entity.asset.car.Information;
import com.pengtu.entity.asset.car.Record;
import com.pengtu.service.asset.CarManager;
import com.pengtu.utils.DateUtils;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

//覆盖默认的nameSpace
@Namespace("/asset/car")
// 定义名为reload的result重定向到CarInfomation.action, 其他result则按照convention默认.
@Results({ @Result(name = BaseActionSupport.RELOAD, location = "record.action", type = "redirect") })
public class RecordAction extends BaseActionSupport<Record> {

	/**
	 * serialVersionUID:TODO
	 *
	 * @since 1.0.0
	 */
	
	private static final long serialVersionUID = 1L;


	private CarManager assetManager;

	@Autowired
	public void setAssetManager(CarManager assetManager) {
		this.assetManager = assetManager;
	}

	private String id;
	private Record carRe;
	private Page<Record> page = new Page<Record>(10);

	public Page<Record> getPage() {
		return page;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		assetManager.deleteCarRe(id);
		return RELOAD;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		// List<PropertyFilter> filters = PropertyFilter
		// .buildFromHttpRequest(Struts2Utils.getRequest());
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = assetManager.searchPageCarRe(page, queryFilter);
		return SUCCESS;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (StringUtils.isNotEmpty(id)) {
			carRe = assetManager.findCarRe(id);
			System.out.println(carRe.getCarInformation()
					.getLicenseplateNumber());
		} else {
			carRe = new Record();
		}
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		carRe.setCarInformation(assetManager.getCarInfo(Struts2Utils
				.getParameter("CARid")));
		assetManager.addCarRe(carRe);
		String mes = "";
		if (StringUtils.isNotEmpty(id)) {
			mes = "修改";
		} else {
			mes = "新增";
		}
		addActionMessage(mes + "车辆使用记录");
		return RELOAD;
	}

	@Override
	public Record getModel() {
		// TODO Auto-generated method stub
		return carRe;
	}

	/**
	 * 将车辆使用记录表转化成json形式
	 * */
	public String recordJson(List<Record> recordList) throws Exception {
		String root = Struts2Utils.getRootPath();
		List<Map<String, Object>> mapRecordList = new ArrayList<Map<String, Object>>();
		for (Record record : recordList) {
			Map<String, Object> p = new HashMap<String, Object>();
			p.put("id", record.getId());
			p.put("title", "使用人：" + record.getUserId());
			p.put("start",
					DateUtils.convertDateToDayString(record.getStartDate()));
			p.put("end",
					DateUtils.convertDateToDayString(record.getFinishDate()));
			p.put("allDay", false);
			p.put("url",
					root + "/asset/car/record!input.action?id="
							+ record.getId());
			p.put("color", this.setRecRed(record));
			mapRecordList.add(p);
		}
		Struts2Utils.renderJson(mapRecordList);
		return NONE;
	}

	/** 查找某一条车辆使用记录，显示该车辆为红色 */
	public String setRecRed(Record record) {
		if (record.getId().equals(id)) {
			return "red";
		} else {
			return com.pengtu.utils.StringUtils.colorMapById(record
					.getCarInformation().getId());
		}
	}

	/**
	 * 
	 * 通过Record id 将某一车牌的使用记录转化成json形式
	 * 
	 * */
	public String getOneCarRecordToJson() throws Exception {
		System.out.println("##########" + id);
		Record rec = assetManager.findCarRe(id);
		List<Record> recordList = assetManager.findCarReByCarNum(rec
				.getCarInformation().getLicenseplateNumber());
		return this.recordJson(recordList);
	}

	/**
	 * 
	 * 通过car信息 id 将某一车牌的使用记录转化成json形式
	 * 
	 * */
	public String getOneRecToJsonByCardId() throws Exception {
		if(StringUtils.isNotEmpty(id)){
			Information carInfo = assetManager.getCarInfo(id);
			List<Record> recordList = assetManager.findCarReByCarNum(carInfo
					.getLicenseplateNumber());
			return this.recordJson(recordList);	
		}
		return null;
	}

	/** 将全部车辆使用记录转化成json形式 */
	public String getAllRecordToJson() throws Exception {
		List<Record> recordList = assetManager.getRecordCar();
		return this.recordJson(recordList);
	}

	public String getSelectListToJson() throws Exception {
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		List<Record> listRecord = new ArrayList<Record>();
		queryFilter.addFilter("delFlag", Constants.DELFLAG_IN, MatchType.EQ);
		listRecord = assetManager.getSelectListToJson(queryFilter);
		return this.recordJson(listRecord);
	}
}
